package org.webball.pitch;

import org.webball.base.abstracts.AbstractBall;
import org.webball.base.abstracts.AbstractPlayer;
import org.webball.base.interfaces.IPitch;
import org.webball.base.interfaces.ITeam;
import org.webball.pitch.phase.BasicPhase;
import org.webball.pitch.phase.Phases;
import org.webball.pitch.phase.PlayPhase;
import org.webball.pitch.phase.TacticalPhase;
import org.webball.tools.MKVector;

/**
 * 
 * @author Maciej Koch
 *
 */
public class Pitch implements IPitch{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7847180645798871262L;
	
	Team[] teams;
	AbstractBall ball;
	
	transient private BasicPhase phase;
	private boolean tacticalPhase;
	private int timeToEndPhase;
	private int won = -1;	
	
	public Pitch() {
		teams = new Team[2];
		ball = new TestBall();
		//only for start
		BasicPhase.attackingTeam = BasicPhase.lastAttackingTeam = Team.T1;
	}
	
	@Override
	public void update() {
				
		phase.update();
		timeToEndPhase = phase.getTimeToEnd();	
		
		isEnd();
	}

	@Override
	public ITeam getTeam(int i) {
		if(i == Team.T1 || i == Team.T2)
			return teams[i];
		else
			return null;
	}

	@Override
	public AbstractBall getBall() {
		return ball;
	}

	@Override
	public boolean isTacticalPhase() {
		return tacticalPhase;
	}

	@Override
	public int getTimetoEndPhase() {
		return timeToEndPhase;
	}

	@Override
	public int isEndOfGame() {
		return won;
	}

	@Override
	public void setTeam(int i, ITeam team) {
		if(i == Team.T1 || i == Team.T2)
			this.teams[i] = (Team) team;
		
		//start
		if(teams[Team.T1] != null && teams[Team.T2] != null)
			setPhase(Phases.TACTICAL);
	}

	@Override
	public AbstractPlayer clickOnPlayer(MKVector mousePosition, int team) {
		if(team != 0 && team != 1) return null;
		deselectAllPlayers(team);
		for(int i = 0 ; i < 6 ; i++) {
			if(teams[team].getPlayer(i).getPosition().distance(mousePosition) < teams[team].getPlayer(i).getSize()/2) {
				teams[team].getPlayer(i).setSelected(true);
				return teams[team].getPlayer(i);
			}
				
		}
		return null;
	}
	@Override
	public void copy(IPitch pitch) {
		setTeam(Team.T1, pitch.getTeam(Team.T1));
		setTeam(Team.T2, pitch.getTeam(Team.T2));	
		this.ball = pitch.getBall();
		this.tacticalPhase = pitch.isTacticalPhase();
		this.won = pitch.isEndOfGame();
		this.timeToEndPhase = pitch.getTimetoEndPhase();
	}
	@Override
	public void deselectAllPlayers(int team)
	{
		for(AbstractPlayer player : teams[team].getAllPlayers())
			player.setSelected(false);
	}
	public void setPhase(short id) {
		
		switch(id) {
		
		case Phases.TACTICAL:
			phase = new TacticalPhase(teams, ball, this);
			tacticalPhase = true;
			return;
		case Phases.PLAY:
			phase = new PlayPhase(teams, ball, this);
			tacticalPhase = false;
			return;
		}	
	}
	//--------------------------------
	//-----------private--------------
	//--------------------------------
	private void isEnd() {
		for(int i = 0 ; i < 2 ; i++)
			if(teams[i].getSetpoints() == 3) {
				won = i;
				return;
			}
		
	}
	

}
